package com.xiaoxin.experience.service.tree.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.xiaoxin.experience.api.http.model.RequestInfo;
import com.xiaoxin.experience.api.tree.model.InstanceInfo;
import com.xiaoxin.experience.util.JSONUtil;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * @author xiaoxin
 */
@Setter
@Getter
@TableName("tbl_instance_info")
public class InstanceEntity implements Serializable
{
    private static final long serialVersionUID = 3749772171390734352L;

    /**
     * 实例id
     */
    @TableId(value = "instance_id",type = IdType.AUTO)
    private Integer instanceId;

    /**
     * 实例名称
     */
    @TableField("instance_name")
    private String instanceName;

    /**
     * 目录id
     */
    @TableField("directory_id")
    private Integer directoryId;

    /**
     * 请求信息
     */
    @TableField("request_info")
    private String requestInfo;

    public InstanceEntity()
    {
    }

    public InstanceEntity(InstanceInfo instanceInfo)
    {
        this.instanceId = instanceInfo.getId();
        this.instanceName = instanceInfo.getName();
        this.directoryId = instanceInfo.getDirectoryId();
        this.requestInfo = JSONUtil.toJson(instanceInfo.getRequestInfo());
    }

    public InstanceInfo toInfo()
    {
        InstanceInfo instanceInfo = new InstanceInfo();
        instanceInfo.setId(instanceId);
        instanceInfo.setName(instanceName);
        instanceInfo.setDirectoryId(directoryId);
        instanceInfo.setRequestInfo(JSONUtil.fromJson(requestInfo, RequestInfo.class));
        return instanceInfo;
    }
}
